import md5 from 'crypto-js/md5';
export default  {
    data(){
        return {
            list: [],
            total: 0,
            filter: {
                page: 1,
                rows: 5,
                title: ""                  
            },
            addAndUpdata: {
                title:"",            //增加内容的字段
                comment: "",         //增加内容的字段
                password: "",        //增加内容的字段
                age:""               //增加内容的字段
            },
            $id: "",
            modal: false,
            modalAdd: false,            
            formValidate: {
              content: "",
              field: ""
            }
        }
    },
    methods:{
      getData() {
        this.$http({
          method: "post",
          url: `http://10.31.160.78:3000/${this.module}/list`,
          data: this.filter
        }).then(res => {
          this.list = res.data.rows;
          this.total = res.data.total;
        });
      },
      search() {
        //按姓名搜索
        // console.log(this.filter.title);
        this.getData();
      },
      onChange(page) {
        this.filter.page = page;
        this.getData();
      },
      pageChange(pageSize) {
        this.filter.rows = pageSize;
        this.getData();
      },
      addContent() {
        //增加按钮
        this.modalAdd = true;
      },
      comfirm() {
        // console.log(this.addAndUpdata);
        //增加
    // password 加密
      this.addAndUpdata.password=(md5(this.addAndUpdata.password).toString()) 
        this.$http({
          method: "post",
          url: `http://10.31.160.78:3000/${this.module}/data`,
          data: this.addAndUpdata
        }).then(res => {
          this.getData();
          this.$Notice.success({
            title: "添加成功"
          });
        });
      },
  
      update(index) {
        //修改弹框
        this.modal = true;
        this.$id = this.list[index]._id;
        // console.log(this.$id);
      },
      ok() {
        //修改确认
        //  console.log(this.formValidate.field);
        //  console.log(this.formValidate.content);
        var field = this.formValidate.field;
        var updataObj = {};
        console.log(field)
      //修改信息时password加密
        if (field=='password') {
          updataObj[field] =md5(this.formValidate.content).toString() ;          
        } else {
        updataObj[field] = this.formValidate.content;        
        }        
        this.$http({
          method: "put",
          url: `http://10.31.160.78:3000/${this.module}/data/` + this.$id,
          data: updataObj
        }).then(res => {
          this.getData();
          this.$Notice.success({
            title: "修改成功"
          });
        });
      },
  
      remove(index) {
        //删除
        // console.log(index);
        // console.log(this.list[index]._id);
        this.$http({
          method: "delete",
          url: `http://10.31.160.78:3000/${this.module}/data/` + this.list[index]._id,
          data: this.filter
        }).then(res => {
          this.getData();
          this.$Notice.success({
            title: "删除成功"
          });
        });
      }
    },
  
    created() {
      this.getData();
    }
    }
